Copper.ScheduleGroup = (function() {
    var _layer;
    var _dummyTask;
    var _startTimeRegulator;
    var _endTimeRegulator;
    
    var _ringShape;
    var _bubbleShape;
    
    return {
        init: function(){
            _layer = new Kinetic.Layer();
            stage.add(_layer);
            
            _ringShape = new Copper.TaskRing({
                isSelected: true,
                startAngle: 0,
                endAngle: 0,
                fillStyle: 'white'
            });
            _layer.add(_ringShape);
            
            _bubbleShape = new Copper.TaskBubble({
                isSelected: true,
                startAngle: 0,
                endAngle: 0,
                title: '',
                content: ''                             
            });
            _layer.add(_bubbleShape);

            _startTimeRegulator = new Copper.TimeRegulator({
                angle: 0,
                mouseMoveHandler: function(newAngle){
                    _ringShape.setStartAngle(newAngle);
                    _bubbleShape.setStartAngle(newAngle); 
                    _layer.draw();   
                }
            });
             
            _layer.add(_startTimeRegulator);
            
            _endTimeRegulator = new Copper.TimeRegulator({
                angle: 0,
                mouseDownHandler: function(newAngle){
                    _ringShape.show();
                    _bubbleShape.show();
                    _layer.draw();  
                },
                mouseMoveHandler: function(newAngle){
                    _ringShape.setEndAngle(newAngle);
                    _bubbleShape.setEndAngle(newAngle);
                    _layer.draw();   
                },
                mouseUpHandler: function(newAngle){
                    //Copper.DummyLayer.hideTimeRegulator(); 
                    _ringShape.hide();
                    _bubbleShape.hide();
                    _layer.draw();
                    
                    var json = Copper.TaskManager.getCurrentTask().getJson();  
                    json.endTime = Copper.Util.angleToTime(newAngle);
                    console.log('Task update:' + json.startTime + '~' + json.endTime);
                    Copper.TaskManager.updateTask(json);
                }
            });
            _layer.add(_endTimeRegulator);   
                        
        },
        
        hideTimeRegulator: function(){
            _startTimeRegulator.hide();
            _endTimeRegulator.hide();
            _ringShape.hide();
            _bubbleShape.hide();
            _layer.draw();
        },
        
        showTimeRegulator: function(task){
            _startTimeRegulator.setAngle(task.startAngle);
            _endTimeRegulator.setAngle(task.endAngle);
            
            _ringShape.setStartAngle(task.startAngle);
            _ringShape.setEndAngle(task.endAngle);
            _ringShape.hide();
            
            _bubbleShape.setStartAngle(task.startAngle);
            _bubbleShape.setEndAngle(task.endAngle);
            _bubbleShape.setContent(task.content);
            _bubbleShape.hide();
            
            _startTimeRegulator.show();
            _endTimeRegulator.show();
            _layer.draw();
        },
        
        refresh: function(){
            _layer.draw();
        }
    }; 
})();